﻿using DevExpress.XtraEditors;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.盘点.Service;
using DXApplicationTangche.Utils;
using DXApplicationTangche.Utils.UC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.纳客
{
    public partial class Form新建报表 : DevExpress.XtraEditors.XtraForm
    {
        public Form新建报表()
        {
            InitializeComponent();
        }
        private DataTable dataTable;
        private void 导入excelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                DataSet result = ExcelUtil.getExcelDatas(openFile.FileName);
                if (result == null)
                {
                    return;
                }
                this.dataTable = result.Tables[0];
                Dictionary<String, ViewStockSpecDTO> valuePairs = 盘点Service.viewStockSpecDTOs();
                List<ViewStockSpecDTO> list = new List<ViewStockSpecDTO>();
                int i = 0;
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    if (i != 0)
                        list.Add(new ViewStockSpecDTO(dataRow, valuePairs));
                    i++;
                }
                this.gridControl新建报表.DataSource = list;
                gridView新建报表.RefreshData();
            }
        }
        public class ViewStockSpecDTO
        {
            public String specNo { get; set; }
            public String orderQty { get; set; }
            public int kaidanCount { get; set; }
            public int baiweinianCount { get; set; }
            public int semiCount { get; set; }
            public int finishCount { get; set; }
            public ViewStockSpecDTO()
            {
            }

            public ViewStockSpecDTO(int kaidanCount, int baiweinianCount, int semiCount, int finishCount)
            {
                this.kaidanCount = kaidanCount;
                this.baiweinianCount = baiweinianCount;
                this.semiCount = semiCount;
                this.finishCount = finishCount;
            }
            public ViewStockSpecDTO(DataRow dataRow, Dictionary<String, ViewStockSpecDTO> valuePairs)
            {
                try
                {
                    this.specNo = dataRow["Column1"].ToString();
                }
                catch { }
                try
                {
                    this.orderQty = dataRow["Column4"].ToString();
                }
                catch
                {
                    this.orderQty = "0";
                }
                if (!String.IsNullOrEmpty(this.specNo))
                {
                    if (valuePairs.ContainsKey(this.specNo))
                    {
                        ViewStockSpecDTO viewStockSpecDTO = valuePairs[this.specNo];
                        this.kaidanCount = viewStockSpecDTO.kaidanCount;
                        this.baiweinianCount = viewStockSpecDTO.baiweinianCount;
                        this.semiCount = viewStockSpecDTO.semiCount;
                        this.finishCount = viewStockSpecDTO.finishCount;
                    }
                    else
                    {
                        this.kaidanCount = 0;
                        this.baiweinianCount = 0;
                        this.semiCount = 0;
                        this.finishCount = 0;
                    }
                }
            }
        }

        private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridView新建报表.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dictionary<String, ViewStockSpecDTO> valuePairs = 盘点Service.viewStockSpecDTOs();
            List<ViewStockSpecDTO> list = new List<ViewStockSpecDTO>();
            int i = 0;
            foreach (DataRow dataRow in this.dataTable.Rows)
            {
                if (i != 0)
                    list.Add(new ViewStockSpecDTO(dataRow, valuePairs));
                i++;
            }
            this.gridControl新建报表.DataSource = list;
            gridView新建报表.RefreshData();
        }
    }
}